home *** CD-ROM | disk | FTP | other *** search
- /* memsort.c - preforms a quicksort with an array of pointers */
- /* and a caller-supplied compare function */
- #include "stdio.h"
- #include "cminor.h"
-
- #define LIMIT 8 /* crossover point for insertion sort */
-
- int memsort(pa,na,pcomp)
- char *pa[] ; /* array of pointers to data to be sorted */
- int na ; /* number of elements to be sorted */
- int (*pcomp) () ; /* pointer to compare function */
- {
- int i,j ; /* indeces for loops */
- char *ptemp ; /* temporary storage for one pointer */
- int nr ; /* number elements in right partition */
- char *ppart ; /* pointer to element used as partition value */
-
- if( na < LIMIT ) /* use insert sort for small partitions */
- { insert(pa,na,pcomp) ;
- return ;
- }
-
- ppart = pa[na/2] ; /* pick middle element for partition */
- i = -1 ; j = na ;
- while( 1==1 )
- { /* find first element to move right */
- do { i = i + 1 ; } while( (*pcomp) (pa[i],ppart) < 0 ) ;
-
- /* find first element to move left */
- do { j = j - 1 ; } while( (*pcomp) (pa[j],ppart) > 0 ) ;
-
- if( i >= j) /* have the boundaries met */
- break ; /* yes - through partitioning */
-
- /* swap i and j elements */
- ptemp = pa[i] ; pa[i] = pa[j] ; pa[j] = ptemp ;
- }
-
- nr = na - i ; /* now deal with each partition */
- if( i < (na/2) )
- { memsort( pa , i , pcomp) ; /* sort left side */
- memsort(&(pa[i]),nr,pcomp) ; /* sort right side */
- }
- else
- { memsort(&(pa[i]),nr,pcomp) ; /* sort right side */
- memsort( pa , i , pcomp) ; /* sort left side */
- }
- }
-
-